package com.situ.staffmgr.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.tomcat.util.http.fileupload.FileUpload;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.situ.staffmgr.entity.Staff;
import com.situ.staffmgr.service.StaffService;
import com.situ.staffmgr.serviceImpl.StaffServiceImpl;
import com.situ.staffmgr.util.BaseServlet;
import com.situ.staffmgr.util.FileUploadUtil;

@WebServlet("/staff.do")
@MultipartConfig
public class StaffServlet extends BaseServlet {
	private StaffService staffService = new StaffServiceImpl();

	public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//获取参数
		String key =request.getParameter("key");
		if (key==null) {
			key="";
		}
		//List staffs = staffService.getAll();
		List staffs = staffService.getLikeNmae(key);
		System.out.println(staffs);
		// 跳转页面
		request.setAttribute("staffs", staffs);
		request.getRequestDispatcher("staff/list.jsp").forward(request, response);
	}

	public void getByDeptId(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.获取参数
		int deptId = Integer.parseInt(request.getParameter("deptId"));
		// 2.业务处理
		List staffs = staffService.getByDeptId(deptId);
		System.out.println(staffs);
		// 3获取数据
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(staffs);
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(json);
	}

	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String idcard = request.getParameter("idcard");
		String strBirthday = request.getParameter("birthday");
		String strDeptId = request.getParameter("deptId");
		String job = request.getParameter("job");
		String strLeaderId = request.getParameter("leaderId");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String strSalary = request.getParameter("salary");
		// 接受图片参数
		Part photo = request.getPart("photo");
		String path = request.getServletContext().getRealPath("upload");
		System.out.println(path);
		String strPhoto = FileUploadUtil.upload(photo, path);
		// 转类型
		Date birthday = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			birthday = sdf.parse(strBirthday);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Integer deptId = null;
		try {
			deptId = Integer.parseInt(strDeptId);
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		Integer leaderId = null;
		try {
			leaderId = Integer.parseInt(strLeaderId);
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		BigDecimal salary = null;
		try {
			salary = new BigDecimal(strSalary);
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		// 封装到一个staff对象
		Staff staff = new Staff();
		staff.setName(name);
		staff.setSex(sex);
		staff.setIdcard(idcard);
		staff.setBirthday(birthday);
		staff.setJob(job);
		staff.setDeptId(deptId);
		staff.setLeaderId(leaderId);
		staff.setPhone(phone);
		staff.setEmail(email);
		staff.setSalary(salary);
		staff.setPhoto(strPhoto);
		System.out.println(staff);
		// 调用业务层
		int result = staffService.add(staff);
		System.out.println(result);

		switch (result) {
		case 1:
			response.sendRedirect("staff/");
			break;
		case -1:
			request.setAttribute("error", "员工名格式不正确");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);
		case -2:
			request.setAttribute("error", "身份证格式不正确");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);

		case -3:
			request.setAttribute("error", "部门号格式不正确");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);

		case -4:
			request.setAttribute("error", "工作职位格式不正确");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);

		case -6:
			request.setAttribute("error", "电话号码格式不正确");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);
		case -7:
			request.setAttribute("error", "邮箱格式不正确");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);

		default:
			request.setAttribute("error", "未知错误，请联系管理员");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);

			break;
		}

	}

	public void remove(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		int id=Integer.parseInt(request.getParameter("id"));
		int res=staffService.remove(id);
		if (res>0) {
			response.sendRedirect("staff/");
		}else if(res==-1){
			response.sendRedirect("staff/?error="+URLEncoder.encode("ID不存在", "UTF-8"));
		}else {
			response.sendRedirect("staff/?error="+URLEncoder.encode("未知错误", "UTF-8"));
			
		}
		
	}

	public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String idcard = request.getParameter("idcard");
		String strBirthday = request.getParameter("birthday");
		String strDeptId = request.getParameter("deptId");
		String job = request.getParameter("job");
		String strLeaderId = request.getParameter("leaderId");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String strSalary = request.getParameter("salary");
		// 接受图片参数
		Part photo = request.getPart("photo");
		String path = request.getServletContext().getRealPath("upload");
		String strPhoto = FileUploadUtil.upload(photo, path);
		// 转类型
		Date birthday = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			birthday = sdf.parse(strBirthday);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Integer deptId = null;
		try {
			deptId = Integer.parseInt(strDeptId);
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		Integer leaderId = null;
		try {
			leaderId = Integer.parseInt(strLeaderId);
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		BigDecimal salary = null;
		try {
			salary = new BigDecimal(strSalary);
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		// 封装到一个staff对象
		Staff staff = new Staff();
		staff.setId(id);
		staff.setName(name);
		staff.setSex(sex);
		staff.setIdcard(idcard);
		staff.setBirthday(birthday);
		staff.setJob(job);
		staff.setDeptId(deptId);
		staff.setLeaderId(leaderId);
		staff.setPhone(phone);
		staff.setEmail(email);
		staff.setSalary(salary);
		staff.setPhoto(strPhoto);
		// 调用业务层
		int result = staffService.edit(staff);
		switch (result) {
		case 1:
			response.sendRedirect("staff/");
			break;
		case -1:
			request.setAttribute("error", "员工名格式不正确");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);
		case -2:
			request.setAttribute("error", "身份证格式不正确");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);

		case -3:
			request.setAttribute("error", "部门号格式不正确");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);

		case -4:
			request.setAttribute("error", "工作职位格式不正确");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);

		case -6:
			request.setAttribute("error", "电话号码格式不正确");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);
		case -7:
			request.setAttribute("error", "邮箱格式不正确");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);

		default:
			request.setAttribute("error", "未知错误，请联系管理员");
			request.getRequestDispatcher("staff/add.jsp").forward(request, response);

			break;
		}
	}

	public void editView(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		int id = Integer.parseInt(request.getParameter("id"));
		Staff staff =staffService.getById(id);
		if (staff==null) {
			response.sendRedirect("../staff/?error="+URLEncoder.encode("ID不存在", "UTF-8"));
			
		}else {
			request.setAttribute("staff", staff);
			request.getRequestDispatcher("staff/edit.jsp").forward(request, response);

		}
	}
}
